//
//  BaseViewController.m
//  fp
//
//  Created by xingshuai on 2020/11/10.
//

#import "BaseViewController.h"

// 个人中心
#import "OneViewController.h"
// 年级选择
#import "GradeFullView.h"
// 年级model
#import "GradModel.h"

@interface BaseViewController () 

// 年级背景
@property (nonatomic, strong) GradeFullView *gradeBackView;
// 年级数组
@property (nonatomic, strong) NSMutableArray *gradeList;

@end

@implementation BaseViewController

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if (self.navigationController.viewControllers.count > 1) {
        // 进入二级页面显示不同样式返回按钮
        if (self.currentnavType == XSCurrentNavViewTypeHome) {
            self.backBtn.hidden = YES;
            self.homeBackBtn.hidden = YES;
        } else if (self.currentnavType == XSCurrentNavViewTypeLesson) {
            self.homeBackBtn.hidden = NO;
            self.backBtn.hidden = YES;
        } else {
            self.backBtn.hidden = self.isHiddenUserBackBtn ? YES : NO;
            self.homeBackBtn.hidden = YES;
        }
    }else{
        self.backBtn.hidden = YES;
        self.homeBackBtn.hidden = YES;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 隐藏系统导航
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 自定义个人中心导航样式
    self.homeNavView.hidden = YES;
    [self creatUserNavUI];
}

// 根据枚举设置不同导航
-(void)setCurrentnavType:(XSCurrentNavViewType)currentnavType {
    _currentnavType = currentnavType;
    switch (currentnavType) {
        case XSCurrentNavViewTypeHome:
            // 自定义首页模块导航样式
            self.homeNavView.hidden = NO;   // 首页模块导航
            self.viewBackImg.hidden = NO;   // 首页页面背景图片
            self.navView.hidden = YES;      // 个人中心导航
            [self creatHomerNavUI];
            self.isHidenrandomSchoolName = NO;  // 显示导航右侧随机学校
            self.lessonLabel.hidden = YES;   // 隐藏校师佳课
            self.headImg.hidden = NO;      // 显示头像
            self.nameLabel.hidden = NO;    // 显示昵称
            break;
        case XSCurrentNavViewTypeUser:
            // 自定义个人中心导航样式
            self.navView.hidden = NO;         // 个人中心导航
            self.viewBackImg.hidden = YES;    // 首页页面背景图片
            self.homeNavView.hidden = YES;    // 首页模块导航
            [self creatUserNavUI];
            self.isHidenrandomSchoolName = NO;  // 显示导航右侧随机学校
            self.lessonLabel.hidden = YES;   // 隐藏校师佳课
            self.headImg.hidden = NO;      // 显示头像
            self.nameLabel.hidden = NO;    // 显示昵称
            break;
        case XSCurrentNavViewTypeLesson:
            // 自定义校师佳课导航样式
            self.homeNavView.hidden = NO;   // 首页模块导航
            self.viewBackImg.hidden = NO;   // 首页页面背景图片
            self.navView.hidden = YES;      // 个人中心导航
            [self creatHomerNavUI];
            self.isHidenrandomSchoolName = YES;  // 隐藏导航右侧随机学校
            self.headImg.hidden = YES;      // 隐藏头像
            self.nameLabel.hidden = YES;    // 隐藏昵称
            self.lessonLabel.hidden = NO;   // 显示校师佳课
            break;
            
        default:
            break;
    }
}

// 初始化个人中心导航样式
-(void)creatUserNavUI {
    [self.view addSubview:self.viewBackImg];
    [self.viewBackImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.view addSubview:self.navView];
    [self.navView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.height.mas_offset(74);
    }];
    
    [self.navView addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.navView);
        make.height.mas_offset(1);
    }];
    
    [self.navView addSubview:self.navLabel];
    [self.navLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.navView);
        make.top.equalTo(self.navView.mas_top).offset(24);
        make.left.equalTo(self.navView.mas_left).offset(70);
        make.right.equalTo(self.navView.mas_right).offset(-70);
        make.bottom.equalTo(self.navView);
    }];
    
    [self.navView addSubview:self.backBtn];
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self.navView);
        make.width.height.mas_offset(CGSizeMake(50, 50));
    }];
}

-(UIView *)navView {
    if (!_navView) {
        _navView = [[UIView alloc] init];
        _navView.backgroundColor = [UIColor whiteColor];
    }
    return _navView;
}

-(UILabel *)navLabel {
    if (!_navLabel) {
        _navLabel = [[UILabel alloc] init];
        _navLabel.text = @"导航标题";
        _navLabel.textAlignment = NSTextAlignmentCenter;
        _navLabel.textColor = [UIColor blackColor];
        _navLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:19];
    }
    return _navLabel;
}

-(UIButton *)backBtn {
    if (!_backBtn) {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setImage:[UIImage imageNamed:@"back_img"] forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
        self.backBtn.hidden = YES;
    }
    return _backBtn;
}

-(UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = customColor(230, 230, 230, 1);
    }
    return _lineView;
}

-(void)setNavTitleColor:(UIColor *)navTitleColor {
    self.navLabel.textColor = navTitleColor;
}

-(void)setUserBackImg:(NSString *)userBackImg {
    [self.backBtn setImage:[UIImage imageNamed:userBackImg] forState:UIControlStateNormal];
}

-(void)clickBack {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)setNavString:(NSString *)navString {
    self.navLabel.text = navString;
}

-(void)setNavColor:(UIColor *)navColor {
    self.navView.backgroundColor = navColor;
}

-(void)setIsHiddenNav:(BOOL)isHiddenNav {
    self.navView.hidden = isHiddenNav;
}

-(void)setIsHiddenUserBackBtn:(BOOL)isHiddenUserBackBtn {
    _isHiddenUserBackBtn = isHiddenUserBackBtn;
    //self.backBtn.hidden = isHiddenUserBackBtn;
}

////////////////////////////////////////////////////

// 初始化首页模块导航样式
-(void)creatHomerNavUI {
    
    // 模拟年级数据
    [self requestGradeData];
    
    [self.view addSubview:self.viewBackImg];
    [self.viewBackImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.view addSubview:self.homeNavView];
    [self.homeNavView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.height.mas_offset(100);
    }];
    
    [self.homeNavView addSubview:self.headImg];
    [self.headImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.homeNavView).offset(15);
        make.top.equalTo(self.homeNavView.mas_top).offset(35);
        make.size.mas_offset(CGSizeMake(50, 50));
    }];
    
    [self.headImg addSubview:self.headBtn];
    [self.headBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.headImg);
    }];
    
    [self.homeNavView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headImg.mas_right).offset(10);
        make.centerY.equalTo(self.headImg);
        make.width.mas_offset(XS_FONT(120));
    }];
    
    [self.homeNavView addSubview:self.gradeBackImg];
    [self.gradeBackImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(25);
        make.centerY.equalTo(self.headImg);
        make.size.mas_offset(CGSizeMake(110, 35));
    }];
    
    [self.gradeBackImg addSubview:self.gradeSignImg];
    [self.gradeSignImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.gradeBackImg.mas_left).offset(7);
        make.top.equalTo(self.gradeBackImg).offset(7);
        make.size.mas_offset(CGSizeMake(26, 26));
    }];
    
    [self.gradeBackImg addSubview:self.gradeLabel];
    [self.gradeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.gradeSignImg.mas_right);
        make.centerY.equalTo(self.gradeBackImg);
        make.right.equalTo(self.gradeBackImg.mas_right).offset(-7);
    }];
    
    [self.homeNavView addSubview:self.randomSchoolName];
    [self.randomSchoolName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.homeNavView.mas_right).offset(-15);
        make.centerY.equalTo(self.headImg);
        make.width.mas_offset(XS_FONT(160));
    }];
    
    [self.homeNavView addSubview:self.searchBackImg];
    [self.searchBackImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.randomSchoolName.mas_left).offset(-30);
        make.centerY.equalTo(self.headImg);
        make.height.mas_offset(35);
        make.left.equalTo(self.gradeBackImg.mas_right).offset(15);
    }];
    
    [self.searchBackImg addSubview:self.searchField];
    [self.searchField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.searchBackImg).offset(31);
        make.top.bottom.equalTo(self.searchBackImg);
        make.right.equalTo(self.searchBackImg).offset(-10);
    }];
    
    [self.searchBackImg addSubview:self.searDefBtn];
    [self.searDefBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.searchBackImg);
    }];
    
    [self.homeNavView addSubview:self.homeBackBtn];
    [self.homeBackBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.homeNavView);
        make.centerY.equalTo(self.headImg);
        make.width.height.mas_offset(CGSizeMake(50, 50));
    }];
    
    [self.homeNavView addSubview:self.lessonLabel];
    [self.lessonLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.homeBackBtn.mas_right);
        make.centerY.equalTo(self.homeBackBtn);
        make.width.mas_offset(XS_FONT(120));
    }];
    
    [kKeyWindow addSubview:self.gradeBackView];
    [self.gradeBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(kKeyWindow);
    }];
}

-(UIView *)homeNavView {
    if (!_homeNavView) {
        _homeNavView = [[UIView alloc] init];
        _homeNavView.backgroundColor = [UIColor clearColor];
    }
    return _homeNavView;
}

-(UIImageView *)headImg {
    if (!_headImg) {
        _headImg = [[UIImageView alloc] init];
        _headImg.image = [UIImage imageNamed:@"def_img"];
        _headImg.layer.cornerRadius = 25;
        _headImg.layer.masksToBounds = YES;
        _headImg.userInteractionEnabled = YES;
    }
    return _headImg;
}

-(UIButton *)headBtn {
    if (!_headBtn) {
        _headBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_headBtn setBackgroundColor:[UIColor clearColor]];
        [_headBtn addTarget:self action:@selector(clickHeadBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headBtn;
}

-(UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.text = @"这是一个小丸子";
        _nameLabel.textColor = [UIColor colorWithHexString:@"#4E75FF"];
        _nameLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:16];
    }
    return _nameLabel;
}

-(UILabel *)randomSchoolName {
    if (!_randomSchoolName) {
        _randomSchoolName = [[UILabel alloc] init];
        _randomSchoolName.text = @"上海市中小学空中课堂";
        _randomSchoolName.textColor = [UIColor colorWithHexString:@"#4E75FF"];
        _randomSchoolName.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:16];
    }
    return _randomSchoolName;
}

-(UIImageView *)searchBackImg {
    if (!_searchBackImg) {
        _searchBackImg = [[UIImageView alloc] init];
        _searchBackImg.image = [UIImage imageNamed:@"homeSearBar"];
        _searchBackImg.userInteractionEnabled = YES;
        _searchBackImg.layer.cornerRadius = 17.5;
        _searchBackImg.layer.masksToBounds = YES;
        
        UIImageView *leftV = [[UIImageView alloc]init];
        leftV.image = [UIImage imageNamed:@"home_Searcg"];
        leftV.contentMode = UIViewContentModeScaleAspectFill;
        leftV.backgroundColor = [UIColor clearColor];
        [_searchBackImg addSubview:leftV];
        [leftV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.searchBackImg).offset(10);
            make.centerY.equalTo(self.searchBackImg);
            make.size.mas_offset(CGSizeMake(16, 16));
        }];
    }
    return _searchBackImg;
}

-(UITextField *)searchField {
    if (!_searchField) {
        _searchField = [[UITextField alloc]init];
        _searchField.backgroundColor = [UIColor clearColor];
        _searchField.textColor = [UIColor whiteColor];
        _searchField.delegate = self;
        _searchField.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:16];
        NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"请输入你需要搜索的主副课" attributes: @{NSForegroundColorAttributeName:[[UIColor whiteColor]  colorWithAlphaComponent:0.5], NSFontAttributeName:_searchField.font}];
        _searchField.attributedPlaceholder = attrString;
        
        [_searchField addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _searchField;
}

-(UIButton *)searDefBtn {
    if (!_searDefBtn) {
        _searDefBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_searDefBtn setBackgroundImage:[UIImage imageNamed:@"homeSearBar"] forState:UIControlStateNormal];
        _searDefBtn.titleLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:15];
        [_searDefBtn addTarget:self action:@selector(clickSearDefBtn) forControlEvents:UIControlEventTouchUpInside];
        _searDefBtn.layer.cornerRadius = 17.5;
        _searDefBtn.layer.masksToBounds = YES;
        [_searDefBtn setImage:[UIImage imageNamed:@"home_Searcg"] forState:UIControlStateNormal];
        [_searDefBtn setTitle:@"请输入你需要搜索的主副课" forState:UIControlStateNormal];
        [_searDefBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _searDefBtn.adjustsImageWhenHighlighted = NO;
        _searDefBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        _searDefBtn.imageView.alpha = 0.5;
    }
    return _searDefBtn;
}

-(UIButton *)gradeBackImg {
    if (!_gradeBackImg) {
        _gradeBackImg = [UIButton buttonWithType:UIButtonTypeCustom];
        [_gradeBackImg setBackgroundImage:[UIImage imageNamed:@"homeGradeBg"] forState:UIControlStateNormal];
        [_gradeBackImg addTarget:self action:@selector(clickGradeBtn:) forControlEvents:UIControlEventTouchUpInside];
        _gradeBackImg.layer.cornerRadius = 17.5;
        _gradeBackImg.layer.masksToBounds = YES;
    }
    return _gradeBackImg;
}

-(UIImageView *)gradeSignImg {
    if (!_gradeSignImg) {
        _gradeSignImg = [[UIImageView alloc] init];
        _gradeSignImg.image = [UIImage imageNamed:@"homeGradeSign"];
    }
    return _gradeSignImg;
}

-(UILabel *)gradeLabel {
    if (!_gradeLabel) {
        _gradeLabel = [[UILabel alloc] init];
        _gradeLabel.text = @"六年级";
        _gradeLabel.textAlignment = NSTextAlignmentCenter;
        _gradeLabel.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
        _gradeLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleSemibold size:16];
    }
    return _gradeLabel;
}

-(UIImageView *)viewBackImg {
    if (!_viewBackImg) {
        _viewBackImg = [[UIImageView alloc] init];
        _viewBackImg.image = [UIImage imageNamed:@"view_bg"];
        _viewBackImg.userInteractionEnabled = YES;
        _viewBackImg.hidden = YES;
    }
    return _viewBackImg;
}

-(UIButton *)homeBackBtn {
    if (!_homeBackBtn) {
        _homeBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_homeBackBtn setImage:[UIImage imageNamed:@"home_back"] forState:UIControlStateNormal];
        [_homeBackBtn addTarget:self action:@selector(clickHomeBack) forControlEvents:UIControlEventTouchUpInside];
        self.homeBackBtn.hidden = YES;
    }
    return _homeBackBtn;
}

-(UILabel *)lessonLabel {
    if (!_lessonLabel) {
        _lessonLabel = [[UILabel alloc] init];
        _lessonLabel.hidden = YES;
        _lessonLabel.textColor = [UIColor colorWithHexString:@"#4E75FF"];
        _lessonLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:22];
    }
    return _lessonLabel;
}

-(GradeFullView *)gradeBackView {
    if (!_gradeBackView) {
        MJWeakSelf
        _gradeBackView = [[GradeFullView alloc]init];
        _gradeBackView.hidden = YES;
        _gradeBackView.arrGradeList = self.gradeList;
        _gradeBackView.didGradeBackViewBlock = ^{
            weakSelf.gradeBackImg.selected = NO;
        };
        _gradeBackView.didGradeCellBlock = ^(NSString * _Nonnull grad) {
            weakSelf.gradeLabel.text = grad;
            weakSelf.gradeBackView.hidden = YES;
            weakSelf.gradeBackImg.selected = NO;
        };
    }
    return _gradeBackView;
}

-(NSMutableArray *)gradeList {
    if (!_gradeList) {
        _gradeList = [[NSMutableArray alloc]init];
    }
    return _gradeList;
}

-(void)requestGradeData {
    [self.gradeList removeAllObjects];
    NSArray *arr = @[@"一年级",@"二年级",@"三年级",@"四年级",@"五年级",@"六年级",@"七年级",@"八年级",@"九年级",@"高一",@"高二",@"高三"];
    for (NSString *str in arr) {
        GradModel *model = [[GradModel alloc]init];
        model.gradeString = str;
        
        [self.gradeList addObject:model];
    }
    
}

-(void)setLessonString:(NSString *)lessonString {
    self.lessonLabel.text = lessonString;
}

-(void)setIsHiddenHomeBackBtn:(BOOL)isHiddenHomeBackBtn {
    self.homeBackBtn.hidden = isHiddenHomeBackBtn;
}

// 首页模块耳机页面返回
-(void)clickHomeBack {
    [self.navigationController popViewControllerAnimated:YES];
}

// 隐藏导航右侧随机学校
-(void)setIsHidenrandomSchoolName:(BOOL)isHidenrandomSchoolName {
    self.randomSchoolName.hidden = isHidenrandomSchoolName;
}

// 导航背景色
-(void)setHomeNavColor:(UIColor *)HomeNavColor {
    self.homeNavView.backgroundColor = HomeNavColor;
}

// 点击头像跳转个人中心
-(void)clickHeadBtn {
    OneViewController *oneVC = [[OneViewController alloc]init];
    [self.navigationController pushViewController:oneVC animated:YES];
}

// 点击年级响应
-(void)clickGradeBtn:(UIButton *)sender {
    NSLog(@"年级");
    sender.selected = !sender.selected;
    self.gradeBackView.hidden = !sender.selected;
}

// 点击首页搜索时进入编辑状态
-(void)clickSearDefBtn {
    self.searDefBtn.hidden = YES;
    [self.searchField becomeFirstResponder];
}

#pragma mark -- UITextFieldDelegate
-(void)textFieldDidEndEditing:(UITextField *)textField {
    self.searDefBtn.hidden = NO;
}

-(void)textChanged:(UITextField *)textField {
    if (textField.text.length > 0) {
        [_searDefBtn setTitle:textField.text forState:UIControlStateNormal];
    } else {
        [_searDefBtn setTitle:@"请输入你需要搜索的主副课" forState:UIControlStateNormal];
    }
}

// 搜索
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField.text.length > 0) {
        if (textField == self.searchField) {
            [self.searchField resignFirstResponder];
            //[_searDefBtn setTitle:textField.text forState:UIControlStateNormal];
            if (_delegate && [_delegate respondsToSelector:@selector(searchTextEndEdit:)]) {
                [_delegate searchTextEndEdit:textField.text];
            }
        }
    } else {
        // 搜索不能为空
    }
    
    
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
